package com.cennavi.bigdata.baotou.scripts;

import com.mongodb.*;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.WKTReader;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created by zuoweiguang on 2017/3/30.
 */
public class GetRticTotalLength {

    private WKTReader wktReader = new WKTReader();
    private MongoClient mongoClient = null;
    private DB db = null;
    private DBCollection trafficStatusCol = null;

    public GetRticTotalLength() {
        ServerAddress serverAddress = new ServerAddress("123.207.109.239", 27017);
        mongoClient = new MongoClient(serverAddress);
        db = mongoClient.getDB("baotou_traffic");
        trafficStatusCol = db.getCollection("traffic_status");
    }

    public void calculateTotalLength() {
        BigDecimal bg = null;
        double total = 0.00;
        DBCursor cursor = trafficStatusCol.find(new BasicDBObject("level", 17), new BasicDBObject("geometry", 1));
        try {
            while (cursor.hasNext()) {
                DBObject rticObj = cursor.next();
                String wkt = (String) rticObj.get("geometry");
                LineString lineString = (LineString) wktReader.read(wkt);
                double length = lineString.getLength();
                bg = new BigDecimal(length).setScale(2, RoundingMode.UP);
//                System.out.println(bg.doubleValue());
                total += bg.doubleValue();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mongoClient.close();
        }
        bg = new BigDecimal(total).setScale(2, RoundingMode.UP);
        System.out.println("rtic total km:" + bg.doubleValue());
    }



    public static void main(String[] args) {
        GetRticTotalLength gtl = new GetRticTotalLength();
        gtl.calculateTotalLength();
    }

}
